package com.sharer.last.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sharer.last.base.enums.BusinessExceptionCode;
import com.sharer.last.base.enums.ExceptionPrefixParamEnum;
import com.sharer.last.base.exception.BusinessException;
import com.sharer.last.mapper.PersonalDetailMapper;
import com.sharer.last.model.dto.persondetail.PersonalDetailUpdateDTO;
import com.sharer.last.model.po.PersonalDetailPO;
import com.sharer.last.model.vo.personDetail.PersonalDetailVO;
import com.sharer.last.service.PersonalDetailService;
import com.sharer.last.utils.BeanUtil;
import com.sharer.last.wrapper.PersonalDetailWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 关于我
 *
 * @ClassName PersonalDetailServiceImpl
 * @Author WangJin
 * @Date 2023/05/09/9:30
 * @Description
 * @Version 1.0
 */
@Service
public class PersonalDetailServiceImpl extends ServiceImpl<PersonalDetailMapper, PersonalDetailPO> implements PersonalDetailService {
    @Resource
    PersonalDetailMapper personalDetailMapper;

    @Resource
    PersonalDetailWrapper personalDetailWrapper;

    /**
     * 获取关于我所有详情
     *
     * @return
     */
    @Override
    public PersonalDetailVO getPersonalDetailInfo() {
        LambdaQueryWrapper<PersonalDetailPO> qw = new LambdaQueryWrapper<>();
        PersonalDetailPO one = this.getOne(qw);
        if (one == null) {
            return PersonalDetailVO.builder().title("帅气的我").build();
        }
        return personalDetailWrapper.entityVO(one);
    }

    /**
     * 更新关于我数据
     *
     * @param personalDetailUpdateDTO 个人详情数据
     * @return
     */
    @Override
    public void updateAbout(PersonalDetailUpdateDTO personalDetailUpdateDTO) {
        Long             id       = personalDetailUpdateDTO.getId();
        PersonalDetailPO detailPO = this.getById(id);
        if (detailPO == null) {
            throw new BusinessException(BusinessExceptionCode.DATA_IS_EMPTY, ExceptionPrefixParamEnum.PERSONAL_DETAIL);
        }
        PersonalDetailPO personalDetailPO = BeanUtil.copy(personalDetailUpdateDTO, PersonalDetailPO.class);
        if (!this.updateById(personalDetailPO)) {
            throw new BusinessException(BusinessExceptionCode.DATA_UPDATE_FAIL, ExceptionPrefixParamEnum.PERSONAL_DETAIL);
        }
    }
}
